// const commonUrl = "http://127.0.0.1:9601/cwqf-api/";   	//本地测试路径
const commonUrl = "http://stjk.whsyzj.com/cwqf-api/";   	//正式路径 

// 处理code=6的通用方法：清空登录状态并跳转登录页
function handleCode6() {
    // 清除登录相关存储
    uni.removeStorageSync("userInfo");
    uni.removeStorageSync("isLogin");
    
    // 获取当前页面路径，避免重复跳转登录页
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const currentPath = currentPage.route;
    
    // 如果当前不在登录页，则跳转
    if (currentPath !== 'pages/login/login') {
        uni.reLaunch({
            url: '/pages/login/login',
            success: () => {
                uni.showToast({ 
                    title: '登录已过期，请重新登录',
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    }
}

// post请求封装
function postRequest(url, data) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: commonUrl + url,
            timeout: 10000,
            data: data,
            sslVerify: false,
            method: "POST",
            header: {
                "content-type": "application/json"
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    // 处理code=6的情况
                    if (res.data?.code === 6) {
                        handleCode6();
                        reject("登录已过期，请重新登录");
                    } else {
                        resolve(res.data);
                    }
                } else {
                    resolve(res.data);
                }
            },
            error: function(e) {
                reject("接口调用error！" + e);
            },
            fail: function(res) {
                reject("接口调用fail！" + JSON.stringify(res.errMsg));
            }
        });
    });
}

// 带token的post请求封装
function postRequestWithToken(url, data) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: commonUrl + url,
            timeout: 10000,
            data: data,
            sslVerify: false,
            method: "POST",
            header: {
                "content-type": "application/json",
                token: uni.getStorageSync("userInfo")?.token || ""
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
                    // 处理code=6的情况
                    if (result?.code === 6) {
                        handleCode6();
                        reject("登录已过期，请重新登录");
                    } else {
                        resolve(result);
                    }
                } else {
                    const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
                    resolve(result);
                }
            },
            error: function(e) {
                reject("接口调用error！" + e);
            },
            fail: function(res) {
                reject("接口调用fail！" + JSON.stringify(res.errMsg));
            }
        });
    });
}

// 带token的文件上传请求
function postFileWithToken(url, files, data) {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: commonUrl + url,
            timeout: 10000,
            files: files,
            formData: data,
            header: {
                token: uni.getStorageSync("userInfo")?.token || ""
            },
            success: function(res) {
                console.info("postFileWithToken ：" + JSON.stringify(res));
                if (res.statusCode == 200) {
                    const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
                    // 处理code=6的情况
                    if (result?.code === 6) {
                        handleCode6();
                        reject("登录已过期，请重新登录");
                    } else {
                        resolve(result);
                    }
                } else {
                    const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
                    resolve(result);
                }
            },
            error: function(e) {
                reject("接口调用error！" + e);
            },
            fail: function(res) {
                reject("接口调用fail！" + JSON.stringify(res.errMsg));
            }
        });
    });
}

// get请求封装
function getRequest(url, data) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: commonUrl + url,
            timeout: 10000,
            data: data,
            method: "GET",
            dataType: "json",
            header: {
                "content-type": "application/json"
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    // 处理code=6的情况
                    if (res.data?.code === 6) {
                        handleCode6();
                        reject("登录已过期，请重新登录");
                    } else {
                        resolve(res.data);
                    }
                } else {
                    resolve(res.data);
                }
            },
            error: function(e) {
                reject("接口调用异常！");
            },
            fail: function(res) {
                reject("接口调用异常！");
            }
        });
    });
}

// 带token的get请求封装
function getRequestWithToken(url, data) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: commonUrl + url,
            timeout: 10000,
            data: {
                data: data
            },
            method: "GET",
            dataType: "json",
            header: {
                token: uni.getStorageSync("userInfo")?.token || ""
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    // 处理code=6的情况
                    if (res.data?.code === 6) {
                        handleCode6();
                        reject("登录已过期，请重新登录");
                    } else {
                        resolve(res.data);
                    }
                } else {
                    resolve(res.data);
                }
            },
            error: function(e) {
                reject("接口调用异常！");
            },
            fail: function(res) {
                reject("接口调用异常！");
            }
        });
    });
}

export default {
    post: postRequest,
    get: getRequest,
    post_token: postRequestWithToken,
    post_file_token: postFileWithToken,
    get_token: getRequestWithToken
};
    